<?php

class ArticleFace extends FaceBase
{
	private $nohf = ['articlesdailymore', 'articlesweeklymore'];
	private $mysql = null;
	private $api = null;
	private $weixin = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header();
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->api = n_api('article');
		$this->weixin = n_api('weixin');
	}

	public function index()
	{
		
	}


	/**
	public function hot_articles_all()
	{
		$stime = strtotime(date('Y-m-d', strtotime('-1 day')));
		$query = "select * from hot_article_all where article_ptime >= $stime order by read_like desc limit 10";
		$hot_articles_all = $this->mysql->select($query);
		$this->_html('hot_articles_all', $hot_articles_all);
	}
	 * 
	 */
	public function articlesDaily(){
		$param = $this->param('
			cid:uint:ignore,
			date:char:ignore,
			page:page
		');
		
		$data['cid'] = empty($param['cid'])?1:$param['cid'];
		$data['page'] = $param['page'];
		$data['perpage'] = $perpage = 20;
		
		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $this->api->getMaxArticleDay();
		}

		
		$data['date'] = date('Y-m-d',$date);
		$start = $perpage*($param['page']-1);
		//$data['wxList'] = $this->api->getRecentArticle($param['cid'],$start,$perpage);
		$data['wxList'] = $this->api->getHotArticleByCategoryId($data['cid'],$date,'day',$start,$perpage);
		$this->_html('articlesDaily', $data);
	}
	public function articlesDailyOld()
	{
		$param = $this->param('
			cid:char:ignore,
			date:char:ignore,
			page:char:ignore
		');

		$data = [];
		$perpage = 10;
		$categories = $this->weixin->getCategories();
		
		if (isset($param['cid'])) {
			$category = $categories[$param['cid']];
		} else {
			$category = current($categories);
		}

		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $this->api->getMaxArticleDay();
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$data['categories'] = $categories;
		$data['date'] = $date;
		$data['total'] = $this->weixin->get_total_count();
		$data['category'] = $category;
		$data['hot_articles'] = $this->api->getHotArticleByCategoryId($category['id'], $date, 'day', $start, $perpage);
		$data['page'] = $page;
		foreach($data['categories'] as $key=>$val){
			$data['cateTree'][$val['first_category']][] = $val;
		}
		$this->_html('articlesDaily', $data);
	}

	public function articlesDailyMore()
	{
		$param = $this->param('
			cid:char:ignore,
			date:char:ignore,
			page:char:ignore
		');
		$perpage = 10;

		if (!isset($param['cid'])) {
			echo json_encode(n_state(false,"暂无一下页"));
			return;
		}

		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = strtotime(date('Y-m-d', strtotime('-1 day')));
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		if ($page > 3) {
			echo json_encode(n_state(false,"暂无更多数据"));
			return;
		}

		$start = ($page - 1) * $perpage;
		$data = $this->api->getHotArticleByCategoryId($param['cid'], $date, 'day', $start, $perpage);
		if (count($data) == 0)
			echo json_encode(n_state(false,"暂无更多数据"));
		else
			$rs = n_state(true,"获取成功");
			$rs['data'] = $data;
			echo json_encode($rs);
	}

	public function articlesWeekly()
	{
		$param = $this->param('
			cid:char:ignore,
			date:char:ignore,
			page:char:ignore
		');
		
		$data = [];
		$perpage = 10;
		$categories = $this->weixin->getCategories();
		
		if (isset($param['cid'])) {
			$category = $categories[$param['cid']];
		} else {
			$category = current($categories);
		}

		$maxWeek = $this->api->getMaxArticleWeek();
		$weekArr = [$maxWeek, $maxWeek-7*24*3600, $maxWeek-2*7*24*2600];
		if (isset($param['date']) && in_array($param['date'], $weekArr)) {
			$date = strtotime($param['date']);
		} else {
			$date = $maxWeek;
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$data['categories'] = $categories;
		$data['date'] = $date;
		$data['dates'] = $weekArr;
		$data['total'] = $this->weixin->get_total_count();
		$data['category'] = $category;
		$data['hot_articles'] = $this->api->getHotArticleByCategoryId($category['id'], $date, 'week', $start, $perpage);
		$data['page'] = $page;
		foreach($data['categories'] as $key=>$val){
			$data['cateTree'][$val['first_category']][] = $val;
		}

		$this->_html('articlesWeekly', $data);
	}

	public function articlesWeeklyMore()
	{
		$param = $this->param('
			cid:char:ignore,
			date:char:ignore,
			page:char:ignore
		');

		$perpage = 10;
		
		if (!isset($param['cid'])) {
			echo json_encode(n_state(false,"暂无一下页"));
			return;
		}

		$maxWeek = $this->api->getMaxArticleWeek();
		$weekArr = [$maxWeek, $maxWeek-7*24*3600, $maxWeek-2*7*24*2600];
		if (isset($param['date']) && in_array($param['date'], $weekArr)) {
			$date = strtotime($param['date']);
		} else {
			$date = $maxWeek;
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		if ($page > 3) {
			echo json_encode(n_state(false,"暂无更多数据"));
			return;
		}

		$start = ($page - 1) * $perpage;
		$data = $this->api->getHotArticleByCategoryId($param['cid'], $date, 'week', $start, $perpage);
		if (count($data) == 0)
			echo json_encode(n_state(false,"暂无更多数据"));
		else
			$rs = n_state(true,"获取成功");
			$rs['data'] = $data;
			echo json_encode($rs);
	}

	public function recentArticle() {
		$data = $this->api->getRecentArticle('报纸');
		$this->_html('recentArticle', $data);
	}

	public function __destruct(){
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->footer();
		}
	}
}